-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add the ability to make the Add to Cart button a mere extern…
…al link UI is not implemented yet and with this change it forces the plugin to use this feature at the moment. Resolves #5
- Loading branch information
Michael Uno
committed
Sep 30, 2023
1 parent
a811a1b
commit 845d86e
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AutoAmazonLinks\WooCommerceProducts\CartToLink; | ||
|
||
use AutoAmazonLinks\WooCommerceProducts\Commons\LoaderAbstract; | ||
|
||
/** | ||
* Converts the cart button to a simple external Amazon link | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
class Loader extends LoaderAbstract { | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @var string | ||
*/ | ||
static public $sDirPath = __DIR__; | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @var string[] A list of component members. | ||
*/ | ||
public $aMembers = [ | ||
__NAMESPACE__ . '\\Events\\CustomCartLinks' => null, | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace AutoAmazonLinks\WooCommerceProducts\CartToLink\Events; | ||
|
||
use AutoAmazonLinks\WooCommerceProducts\Commons\MemberInterface; | ||
|
||
/** | ||
* Provides means to customize the Add-to-Cart button links | ||
* | ||
* Some users might want just link to the product page rather than adding the product to the on-site cart. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
class CustomCartLinks implements MemberInterface { | ||
|
||
/** | ||
* @since 1.1.0 | ||
*/ | ||
public function run() { | ||
|
||
add_filter( 'woocommerce_loop_add_to_cart_link', [ $this, 'replyToEditAddToCartButtonLinkInLoops' ], 100, 3); | ||
|
||
// Add to Cart text | ||
add_filter( 'woocommerce_product_single_add_to_cart_text', [ $this, 'replyToEditAddToCartTextSingle' ], 100, 2 ); | ||
add_filter( 'woocommerce_product_add_to_cart_text', [ $this, 'replyToEditAddToCartTextInLoops' ], 100, 2 ); | ||
|
||
// Replace the Add to Cart form with a button | ||
add_filter( 'woocommerce_before_add_to_cart_form', [ $this, 'replyToAddBeforeCartForm' ] ); | ||
add_filter( 'woocommerce_after_add_to_cart_form', [ $this, 'replyToAddAfterCartForm' ] ); | ||
|
||
// Add to Cart url | ||
add_filter( 'woocommerce_product_add_to_cart_url', [ $this, 'replyToEditAddToCartURL' ], 10, 2 ); | ||
|
||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @param \WC_Product $oProduct | ||
* @return string The modified `<a>` tag. | ||
*/ | ||
public function replyToEditAddToCartButtonLinkInLoops( $sLink, $oProduct, $aArguments ) { | ||
return "<a href='" . esc_url( $this->___getCartHref( $oProduct ) ) . "' rel='nofollow' target='_blank' class='button product_type_simple add_to_cart_button text_replaceable' title='" . esc_attr( $oProduct->get_title() ) . "' >" | ||
. $this->___getCartButtonLabel() | ||
. "</a>"; | ||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @return string | ||
*/ | ||
public function replyToEditAddToCartTextSingle( $sButtonText, $oProduct ) { | ||
return $this->___getCartButtonLabel(); | ||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @return string | ||
*/ | ||
public function replyToEditAddToCartTextInLoops( $sButtonText, $oProduct ) { | ||
return $this->___getCartButtonLabel(); | ||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @return void | ||
*/ | ||
public function replyToAddBeforeCartForm() { | ||
echo "<div class='add-to-cart-form' style='display:none;'>"; | ||
} | ||
/** | ||
* @since 1.1.0 | ||
* @return void | ||
*/ | ||
public function replyToAddAfterCartForm() { | ||
echo "</div>"; // end hidden | ||
|
||
if ( empty( $GLOBALS[ 'product' ] ) ) { | ||
return; | ||
} | ||
|
||
// Add the "Add to Cart" link | ||
echo "<form class='cart'>" | ||
. "<a href=" . esc_url( $this->___getCartHref( $GLOBALS[ 'product' ] ) ) . " class='button alt' rel='nofollow' target='_blank' title='" . esc_attr( $GLOBALS[ 'product' ]->get_title() ) . "'>" | ||
. $this->___getCartButtonLabel() | ||
. "</a>" | ||
. "</form>"; | ||
} | ||
|
||
/** | ||
* Modifies the Add to Cart URL | ||
* | ||
* The Store Front WooCommerce default theme adds a sticky form at the top and this filter is needed for it. | ||
* | ||
* @param string $sPermalink | ||
* @param \WC_Product $oProduct | ||
* @return string | ||
* @sinec 1.1.0 | ||
*/ | ||
public function replyToEditAddToCartURL( $sPermalink, $oProduct ) { | ||
return $this->___getCartHref( $oProduct ); | ||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @return string | ||
*/ | ||
private function ___getCartButtonLabel() { | ||
return __( 'Buy Now', 'amazon-auto-links' ); | ||
} | ||
|
||
/** | ||
* @since 1.1.0 | ||
* @param \WC_Product $oProduct | ||
* @return string | ||
*/ | ||
private function ___getCartHref( $oProduct ) { | ||
$_oUtil = new \AmazonAutoLinks_PluginUtility(); | ||
$_sSKU = $oProduct->get_sku(); // ASIN|locale|currency|language | ||
$_aItem = explode( '|', $_sSKU ); | ||
$_sASIN = $_oUtil->getElement( $_aItem, 0 ); | ||
$_sLocale = $_oUtil->getElement( $_aItem, 1 ); | ||
$_oOption = \AmazonAutoLinks_Option::getInstance(); | ||
$_oLocale = new \AmazonAutoLinks_Locale( $_sLocale ); | ||
return $_oLocale->getMarketPlaceURL( 'dp/' . $_sASIN . '/?tag=' . $_oOption->getAssociateID( $_sLocale ) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters